﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AnalysisServices;
using System.Threading;
using System.Text.RegularExpressions;
using System.Configuration;

namespace ParallelCubeProcessor
{
    class Program
    {
        static void Main(string[] args)
        {
            string serverName = ConfigurationManager.AppSettings["server"].ToString();
            int maxConcurrent = int.Parse(ConfigurationManager.AppSettings["maxConcurrentThreads"].ToString());
            string databaseNameMatchPattern = ConfigurationManager.AppSettings["databaseNameMatchPattern"].ToString();
            ParallelProcessingType pt = (ParallelProcessingType)Enum.Parse(typeof(ParallelProcessingType), ConfigurationManager.AppSettings["parallelProcessingType"].ToString());
            Server server = new Server();
            server.Connect(serverName);

            ParallelProcessingHandler pph = ParallelProcessingHandlerFactory.GetParallelProcessingHandler(pt, maxConcurrent, serverName);

            System.Console.WriteLine(string.Format("[{0}] Starting Database Iteration", DateTime.Now.ToLongTimeString()));

            foreach (Database db in server.Databases)
            {
                if (Regex.IsMatch(db.Name, databaseNameMatchPattern))
                {
                    pph.ProcessDatabase(db);
                }
                else
                {
                    System.Console.WriteLine(string.Format("Ignoring Database: {0}", db.Name));
                }
            }
            server.Disconnect();
            System.Console.WriteLine(string.Format("[{0}] Database Iteration Complete", DateTime.Now.ToLongTimeString()));
            
            pph.FinishProcessing();
            System.Console.WriteLine(string.Format("[{0}] All Processing Complete", DateTime.Now.ToLongTimeString()));
        }
    }
}
